//
//  LoginViewController.m
//  Nurse
//
//  Created by RainSets on 12/11/13.
//  Copyright (c) 2013 RainSets. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"
#import "OperatorService.h"
#import "MBProgressHUD.h"
#import "SVProgressHUD.h"
@interface LoginViewController ()

@property(nonatomic,weak)IBOutlet UITextField *fdUserName;
@property(nonatomic,weak)IBOutlet UITextField *fdPWD;


@end

@implementation LoginViewController

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    //判断用户名工号输入4位后自动跳转焦点。
    if (range.location>3)
        
    {
        [self.fdPWD becomeFirstResponder];
        
        return NO;
    }
    else
    {
        return YES;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self.fdUserName becomeFirstResponder];
    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"login-bg.png"]];
    self.fdUserName.delegate=self;
  
    
    // Do any additional setup after loading the view from its nib.
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(IBAction)loginClick:(id)sender{
    //AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    NSString* username = [self.fdUserName text];
    NSString* password = [self.fdPWD text];
    //引用SVProgressHUD 出现加载等待框。
    [SVProgressHUD showWithStatus:@"登录中..." maskType:SVProgressHUDMaskTypeBlack];
    //对话框显示时需要执行的操作
    [[OperatorService sharedInstance]checkLoginForUsername:username andPassword:password withSuccessHandler:^(id result) {
        [SVProgressHUD dismiss];
        //NSLog(@"login result class:%@",[result class]);
        NSLog(@"login result:%@",[result valueForKey:@"科室"]);
        
        NSString* bqCodesStr = [result valueForKey:@"科室"];
        __block NSArray *split = [bqCodesStr componentsSeparatedByString:@","];
        [[OperatorService sharedInstance]setCurrentOperator:result];
        [[[OperatorService sharedInstance]currentOperatorWards]  removeAllObjects];
        
        if ([split count]>0) {
            __block int index = 0;
            NSString* wardCode = [split objectAtIndex:index];
            FailHandler failHandler = ^(NSError *error, NSString *title){
                [[DataEngine sharedInstance]showMessage:[error description] withTitle:title];
            };
    
            //block 递归 为避免内存泄漏 使用 strong weak dance
             SuccessHandler successHandler ;
            __block __weak SuccessHandler successHandlerWeak;
             successHandlerWeak = successHandler = ^(id result){
                [[[OperatorService sharedInstance]currentOperatorWards] addObject:result];
                index++;
                NSLog(@"index:%d",index);
                if (index < [split count ]) {
                    NSString* wardCode2 = [split objectAtIndex:index];
                    //NSLog(@"wardCode2:%@",wardCode2);
                    [[OperatorService sharedInstance]queryDepartmentNameForCode:wardCode2 withSuccessHandler:successHandlerWeak andFailHandler:failHandler];
                }else{
                    [App pushTabbar];
                }
            };
            
            [[OperatorService sharedInstance]setCurrentOperatorWardCode:[split objectAtIndex:0]];            
            [[OperatorService sharedInstance]queryDepartmentNameForCode:wardCode withSuccessHandler:successHandler andFailHandler:failHandler];
            

        }else{
             [[OperatorService sharedInstance]setCurrentOperatorWardCode:nil];
        }
        //[App pushTabbar];
    } andFailHandler:^(NSError* error,NSString* title){
        [SVProgressHUD dismiss];
        [[DataEngine sharedInstance]showMessage:[error description] withTitle:title];
    }];
    

    
    
}


- (IBAction)Mm_return:(id)sender {
    NSString* username = [self.fdUserName text];
    NSString* password = [self.fdPWD text];
    //引用SVProgressHUD 出现加载等待框。
    [SVProgressHUD showWithStatus:@"登录中..." maskType:SVProgressHUDMaskTypeBlack];
    //对话框显示时需要执行的操作
    [[OperatorService sharedInstance]checkLoginForUsername:username andPassword:password withSuccessHandler:^(id result) {
        [SVProgressHUD dismiss];
        //NSLog(@"login result class:%@",[result class]);
        NSLog(@"login result:%@",[result valueForKey:@"科室"]);
        
        NSString* bqCodesStr = [result valueForKey:@"科室"];
        __block NSArray *split = [bqCodesStr componentsSeparatedByString:@","];
        [[OperatorService sharedInstance]setCurrentOperator:result];
        [[[OperatorService sharedInstance]currentOperatorWards]  removeAllObjects];
        
        if ([split count]>0) {
            __block int index = 0;
            NSString* wardCode = [split objectAtIndex:index];
            FailHandler failHandler = ^(NSError *error, NSString *title){
                [[DataEngine sharedInstance]showMessage:[error description] withTitle:title];
            };
            
            //block 递归 为避免内存泄漏 使用 strong weak dance
            SuccessHandler successHandler ;
            __block __weak SuccessHandler successHandlerWeak;
            successHandlerWeak = successHandler = ^(id result){
                [[[OperatorService sharedInstance]currentOperatorWards] addObject:result];
                index++;
                NSLog(@"index:%d",index);
                if (index < [split count ]) {
                    NSString* wardCode2 = [split objectAtIndex:index];
                    //NSLog(@"wardCode2:%@",wardCode2);
                    [[OperatorService sharedInstance]queryDepartmentNameForCode:wardCode2 withSuccessHandler:successHandlerWeak andFailHandler:failHandler];
                }else{
                    [App pushTabbar];
                }
            };
            
            [[OperatorService sharedInstance]setCurrentOperatorWardCode:[split objectAtIndex:0]];
            [[OperatorService sharedInstance]queryDepartmentNameForCode:wardCode withSuccessHandler:successHandler andFailHandler:failHandler];
            
            
        }else{
            [[OperatorService sharedInstance]setCurrentOperatorWardCode:nil];
        }
        //[App pushTabbar];
    } andFailHandler:^(NSError* error,NSString* title){
        [SVProgressHUD dismiss];
        [[DataEngine sharedInstance]showMessage:[error description] withTitle:title];
    }];
    

}
@end
